package com.leonzhangxf.ioc.event.publication;

import lombok.Data;
import lombok.NonNull;
import org.springframework.context.ApplicationEvent;
import org.springframework.core.ResolvableType;
import org.springframework.core.ResolvableTypeProvider;

/**
 * A entity event with generic parameter and property.
 *
 * @author leonzhangxf
 */
public class EntityEvent<T extends Object> extends ApplicationEvent implements ResolvableTypeProvider {

    private T entity;

    /**
     * Create a new ApplicationEvent.
     *
     * @param source the object on which the event initially occurred (never {@code null})
     */
    public EntityEvent(Object source, T entity) {
        super(source);
        this.entity = entity;
    }

    public T getEntity() {
        return entity;
    }

    @Override
    public ResolvableType getResolvableType() {
        return ResolvableType.forClassWithGenerics(getClass(), ResolvableType.forInstance(getSource()));
    }

    /**
     * A demo entity.
     */
    @Data
    public static class Entity {

        @NonNull
        private String message;
    }

    /**
     * A demo simple entity.
     */
    @Data
    public static class SimpleEntity {

        @NonNull
        private String message;
    }
}
